package com.nstc.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.nstc.demo.dto.AimsAccountInfo;
import com.nstc.demo.dto.AimsAccountQueryParam;
import com.nstc.demo.entity.AimsAccount;
import com.nstc.demo.service.AimsAccountService;
import com.nstc.demo.util.Result;
import com.nstc.demo.util.ResultGenerator;

/**
 * 
 * @ClassName: AimsAccountController
 * @Description: Aims_Account表的增删改查接口
 * @author fbb
 * @date 2021年7月30日
 *
 */
@RestController
@RequestMapping("/aimsaccount")
public class AimsAccountController {

	@Autowired
	private AimsAccountService aimsAccountService;
	private Logger logger=LoggerFactory.getLogger(AimsAccountController.class);

	/**
	 * 
	 * @Title: getById
	 * @Description: 根据主键查询
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/getbyid", method = RequestMethod.GET)
	public Result getById(@RequestParam int id) {
		AimsAccountInfo accountInfo = aimsAccountService.getInfoById(id);
		return ResultGenerator.genSuccessResult(accountInfo);
	}

	/**
	 * 
	 * @Title: getPageWithCondition
	 * @Description: 获取分页数据，可携带查询条件
	 * @param page                  起始页
	 * @param size                  页大小
	 * @param aimsAccountQueryParam 查询条件包装对象
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/getpagewithcondition", method = RequestMethod.POST)
	public Result getPageWithCondition(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer size, AimsAccountQueryParam aimsAccountQueryParam) {
		PageInfo<AimsAccount> pageInfo = aimsAccountService.getPageWithCondition(page, size, aimsAccountQueryParam);
		return ResultGenerator.genSuccessResult(pageInfo);
	}

	/**
	 * 
	 * @Title: add
	 * @Description: 新增
	 * @param aimsAccount
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result add(@RequestBody @Validated AimsAccount aimsAccount) {
		long id = aimsAccountService.add(aimsAccount);
		return ResultGenerator.genSuccessResult(id);
	}

	/**
	 * 
	 * @Title: update
	 * @Description: 修改
	 * @param aimsAccount
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@RequestBody AimsAccount aimsAccount) {
		boolean isSuccess = aimsAccountService.update(aimsAccount);
		if (!isSuccess) {
			return JSON.toJSONString(ResultGenerator.genFailResult("操作失败！"));
		}
		return JSON.toJSONString(ResultGenerator.genSuccessResult("操作成功！"));
	}

	/**
	 * @Title: delete
	 * @Description: 删除
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delete(@RequestParam int id) {
		boolean isSuccess = aimsAccountService.deleteById(id);
		if (!isSuccess) {
			return JSON.toJSONString(ResultGenerator.genFailResult("操作失败"));
		}
		return JSON.toJSONString(ResultGenerator.genSuccessResult("操作成功！"));
	}

}
